package xiaodou.data.deal
{
	import flash.events.EventDispatcher;
	import flash.events.IEventDispatcher;
	
	import xiaodou.events.DealEvent;
	
	public class DealedWeiboModel extends EventDispatcher
	{
		private var _dealedArray:Array;
		public function DealedWeiboModel(target:IEventDispatcher=null)
		{
			super(target);
		}
		
		public function add(dealWeibo:DealedWeiboData):void
		{
			var index:int = getItemIndexByWeiboId(dealWeibo.weiboId);
			if(index >= 0)
				_dealedArray.splice(index, 1);
			_dealedArray.push(dealWeibo);
			dispatchEvent(new DealEvent(DealEvent.ADD, dealWeibo));
		}
		
		public function remove(weiboId:String):DealedWeiboData
		{
			var index:int = getItemIndexByWeiboId(weiboId);
			if(index >= 0)
			{
				var result:DealedWeiboData = _dealedArray.splice(index, 1)[0];
				dispatchEvent(new DealEvent(DealEvent.REMOVE, result));
				return result;
			}
			return null;
		}
		
		public function getItemIndexByWeiboId(weiboId:String):int
		{
			for (var i:int = 0; i < _dealedArray.length; i++) 
			{
				var item:DealedWeiboData = _dealedArray[i];
				if(item.weiboId == weiboId)
					return i;
			}
			return -1;
			
		}

		public function get dealedArray():Array
		{
			return _dealedArray;
		}

		public function set dealedArray(value:Array):void
		{
			_dealedArray = value;
			dispatchEvent(new DealEvent(DealEvent.ADD, null));
		}

	}
}